<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><title>Dockerfile学习 | 飞の博客</title><meta name="author" content="Fei"><meta name="copyright" content="Fei"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="ffffff"><meta name="description" content="Dockerfile学习">
<meta property="og:type" content="article">
<meta property="og:title" content="Dockerfile学习">
<meta property="og:url" content="https://tengfei2233.gitee.io/2023/8/27/Dockerfile%E5%AD%A6%E4%B9%A0/index.html">
<meta property="og:site_name" content="飞の博客">
<meta property="og:description" content="Dockerfile学习">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-6-26/docker.jpg">
<meta property="article:published_time" content="2023-08-27T15:34:37.000Z">
<meta property="article:modified_time" content="2023-08-27T15:34:37.000Z">
<meta property="article:author" content="Fei">
<meta property="article:tag" content="Docker">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-6-26/docker.jpg"><link rel="shortcut icon" href="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/common/icon.png"><link rel="canonical" href="https://tengfei2233.gitee.io/2023/8/27/Dockerfile%E5%AD%A6%E4%B9%A0/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="msvalidate.01" content="2ADFA30867CBC6E090E3C5B244DC311D"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.3.0/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.staticfile.org/fancyapps-ui/4.0.31/fancybox.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?07a1f58fb95d3d4a97c2ae0a181c9964";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.staticfile.org/flickr-justified-gallery/2.1.2/fjGallery.min.js',
      css: 'https://cdn.staticfile.org/flickr-justified-gallery/2.1.2/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Dockerfile学习',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-08-27 23:34:37'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', 'ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>const preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> { preloader.endLoading() })

if (false) {
  document.addEventListener('pjax:send', () => { preloader.initLoading() })
  document.addEventListener('pjax:complete', () => { preloader.endLoading() })
}</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/common/fei.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">24</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">28</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">16</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" target="_blank" rel="noopener" href="https://github.com/tengfei2233"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-6-26/docker.jpg')"><nav id="nav"><span id="blog-info"><a href="/" title="飞の博客"><span class="site-name">飞の博客</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" target="_blank" rel="noopener" href="https://github.com/tengfei2233"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Dockerfile学习</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-08-27T15:34:37.000Z" title="发表于 2023-08-27 23:34:37">2023-08-27</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-08-27T15:34:37.000Z" title="更新于 2023-08-27 23:34:37">2023-08-27</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Docker/">Docker</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3.8k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>13分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Dockerfile学习"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="Dockerfile学习"><a href="#Dockerfile学习" class="headerlink" title="Dockerfile学习"></a>Dockerfile学习</h2><h5 id="下一篇Docker文章：Docker-Compose学习"><a href="#下一篇Docker文章：Docker-Compose学习" class="headerlink" title="下一篇Docker文章：Docker Compose学习"></a>下一篇Docker文章：<a target="_blank" rel="noopener" href="https://blog.tengfei.tech/2023/8/27/Docker-Compose%E5%AD%A6%E4%B9%A0/"><font color="red">Docker Compose学习</font></a></h5><h3 id="什么是Dockerfile"><a href="#什么是Dockerfile" class="headerlink" title="什么是Dockerfile"></a>什么是Dockerfile</h3><blockquote>
<p>Dockerfile是一种用于定义Docker镜像构建过程的文本文件，通过一系列指令和参数指导Docker引擎执行操作。它提供了可重复、可自动化的方式来构建镜像，包括安装软件包、复制文件、设置环境变量等。Dockerfile具有版本控制、可移植性和自动化构建等优势，使得应用程序的部署和交付更加简单和可靠。通过编写清晰、可维护的Dockerfile，开发人员可以轻松构建、交付和部署应用程序，提高团队协作效率和应用程序的可靠性。</p>
<p>简单来说，Dockerfile就是一个安装指令集合，通过这个指令集合，将指定目录下的文件打包成一个Docker镜像；因此我们可以通过这个文件构建我们想要的镜像。</p>
</blockquote>
<h3 id="Dockerfile指令详解"><a href="#Dockerfile指令详解" class="headerlink" title="Dockerfile指令详解"></a>Dockerfile指令详解</h3><h4 id="FROM"><a href="#FROM" class="headerlink" title="FROM"></a>FROM</h4><blockquote>
<p>构建镜像的基础镜像，解析来的一切操作都在这个基础镜像上进行。</p>
<p>什么是基础镜像呢？就拿<code>FROM ubuntu:latest</code>来说，它使用ubuntu作为基础镜像，这里的ubuntu是指包含linux内核和ubuntu特征的小型操作系统。我们要知道，一个服务的运行必须依赖一个操作系统，Docker已经隔离了宿主机操作系统，那么每一个镜像就是一个封装了操作系统的微型环境，这个环境里面运行着我们需要的服务，这个操作系统是非常轻量化的，只包含Linux内核和一些工具类，包管理器等；我们也可以根据构建需求进行调整。</p>
<p>像Ubuntu、Alpine、CentOS这样的基础镜像叫做基础操作系统镜像，也有特定环境的镜像，如：Java、Node.js、Python环境的镜像，这种镜像除了操作系统本身外，就只安装了运行对应语言的环境。</p>
<p><strong>因此在选择镜像的时候也有一些讲究：</strong></p>
<ol>
<li>尽可能小的镜像：选择尽可能小的基础镜像可以减少镜像的大小，提高构建速度和部署效率。较小的镜像通常只包含必需的组件，不包含多余的软件包和依赖项。</li>
<li>官方支持和维护：选择官方提供的基础镜像可以获得更好的支持和维护，确保镜像的安全性和稳定性。官方镜像经过广泛测试和验证，有活跃的社区支持。</li>
<li>特定用途的镜像：如果应用程序有特定的需求或依赖项，可以选择特定用途的基础镜像。例如，如果应用程序使用Java开发，可以选择官方提供的Java镜像作为基础镜像。</li>
</ol>
<p>通过上面我们发现镜像的构建其实可以一层又一层：我们先用基础镜像a构建生成镜像b，再用镜像b构建生成镜像c。</p>
</blockquote>
<h4 id="MAINTAINER（已弃用）-x2F-LABEL"><a href="#MAINTAINER（已弃用）-x2F-LABEL" class="headerlink" title="MAINTAINER（已弃用）&#x2F; LABEL"></a>MAINTAINER（已弃用）&#x2F; LABEL</h4><blockquote>
<p><code>MAINTAINER</code>用于指定镜像作者信息，<code>LABEL</code>用于给容器指定元数据信息</p>
<p>在较早的Docker版本中，使用<code>MAINTAINER</code>指令来指定镜像的作者信息。然而，自Docker 1.13版本开始，<code>MAINTAINER</code>指令已被标记为废弃（deprecated），并建议使用<code>LABEL</code>指令来替代。</p>
<p>例如：</p>
<figure class="highlight dockerfile"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 指定容器作者</span></span><br><span class="line"><span class="keyword">MAINTAINER</span> 张三</span><br><span class="line"></span><br><span class="line"><span class="comment"># LABEL的使用方式</span></span><br><span class="line"><span class="keyword">LABEL</span><span class="language-bash"> maintainer=<span class="string">&quot;张三&quot;</span></span></span><br><span class="line"><span class="keyword">LABEL</span><span class="language-bash"> version=<span class="string">&quot;1.0&quot;</span></span></span><br><span class="line"><span class="keyword">LABEL</span><span class="language-bash"> description=<span class="string">&quot;这是一段描述&quot;</span></span></span><br></pre></td></tr></table></figure>
</blockquote>
<h4 id="RUN"><a href="#RUN" class="headerlink" title="RUN"></a>RUN</h4><blockquote>
<p>用于在容器中运行指令，因为上文说了，容器就是一个小型Linux系统，如果我在构建镜像的时候需要在这个Linux系统上运行一些指令，就需要用到RUN了，例如：</p>
<figure class="highlight dockerfile"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 在该容器中更新apt包和安装net-tools</span></span><br><span class="line"><span class="keyword">RUN</span><span class="language-bash"> apt-get update &amp;&amp; apt-get install -y net-tools</span></span><br></pre></td></tr></table></figure>

<p>**注意：**在使用RUN指令的时候，不要一个命令就使用一个RUN指令，这样会增加容器层数；因为每使用一次RUN指令的时候，容器在构建过程中就会产生一个中间层，一是会减慢容器的构建速度，二是增加容器大小；对于有多个命令的情况，可以像上面一样，将多个命令用&amp;&amp;拼接在一起，对于换行的命令，可以使用\</p>
</blockquote>
<h4 id="CMD"><a href="#CMD" class="headerlink" title="CMD"></a>CMD</h4><blockquote>
<p>CMD指令用于设置容器在启动是运行的命令，在镜像构建阶段CMD是不会执行的，当我们把镜像运行成容器，在容器启动阶段，CMD指定的命令就会运行。</p>
<p>CMD命令的两种编写形式：</p>
<p>Exec形式（推荐）：</p>
<figure class="highlight dockerfile"><table><tr><td class="code"><pre><span class="line"><span class="keyword">CMD</span><span class="language-bash"> [<span class="string">&quot;可执行文件&quot;</span>, <span class="string">&quot;参数1&quot;</span>, <span class="string">&quot;参数2&quot;</span>, ...]</span></span><br><span class="line"><span class="comment"># 例如执行nodejs服务，执行node app.js</span></span><br><span class="line"><span class="keyword">CMD</span><span class="language-bash"> [<span class="string">&quot;node&quot;</span>,<span class="string">&quot;app.js&quot;</span>]</span></span><br><span class="line"><span class="comment"># 命令以json数组的形式</span></span><br></pre></td></tr></table></figure>

<p>Shell形式：</p>
<figure class="highlight dockerfile"><table><tr><td class="code"><pre><span class="line"><span class="keyword">CMD</span><span class="language-bash"> 可执行脚本</span></span><br><span class="line"><span class="comment"># 例如执行nodejs服务</span></span><br><span class="line"><span class="keyword">CMD</span><span class="language-bash"> node app.js</span></span><br><span class="line"><span class="comment"># 适用需要使用到shell变量的形式</span></span><br></pre></td></tr></table></figure>

<p><strong>注意：</strong><code>CMD</code> 指令可以在 Dockerfile 中只出现一次。如果多次出现，只有最后一条 <code>CMD</code> 指令会生效，指定的命令将成为容器启动时的默认命令。如果在运行容器时提供了命令行参数，它们将覆盖 <code>CMD</code> 中指定的默认命令。</p>
</blockquote>
<h4 id="ENTRYPOINT"><a href="#ENTRYPOINT" class="headerlink" title="ENTRYPOINT"></a>ENTRYPOINT</h4><blockquote>
<p>ENTRYPOINT指令的用法以及效果跟CMD类似，还是有一些区别的：</p>
<ul>
<li>CMD命令会被运行容器是外部指定的参数所替代，就是我在运行容器的时候，给它设置了运行参数，将覆盖CMD指令后的参数；而ENTRYPOINT不会覆盖，而是替换。</li>
<li>一个镜像内可以有多个CMD指令，但只能有一个ENTRYPOINT指令</li>
</ul>
<p>例如：</p>
<figure class="highlight dockerfile"><table><tr><td class="code"><pre><span class="line"><span class="keyword">FROM</span> ubuntu</span><br><span class="line"><span class="keyword">CMD</span><span class="language-bash"> [<span class="string">&quot;echo&quot;</span>, <span class="string">&quot;Hello, Docker!&quot;</span>]</span></span><br></pre></td></tr></table></figure>

<p>结果输出：Hello, Docker!</p>
<figure class="highlight dockerfile"><table><tr><td class="code"><pre><span class="line"><span class="keyword">FROM</span> ubuntu</span><br><span class="line"><span class="keyword">ENTRYPOINT</span><span class="language-bash"> [<span class="string">&quot;echo&quot;</span>, <span class="string">&quot;Hello, Docker!&quot;</span>]</span></span><br></pre></td></tr></table></figure>

<p>结果输出：echo Hello, Docker!</p>
<p><strong>在容器中也是更建议使用CMD命令</strong></p>
</blockquote>
<h4 id="EXPOSE"><a href="#EXPOSE" class="headerlink" title="EXPOSE"></a>EXPOSE</h4><blockquote>
<p>申明容器内监听的端口，多个端口用逗号拼接；什么叫申明监听端口呢，指的是我给这个端口做一个标记，但是我不会去监听这个端口，只是做一个标记，当创建容器时可以使用-p参数将该端口与宿主机进行映射，比如</p>
<figure class="highlight dockerfile"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 表明我申明了80端口，在创建容器的时候可以使用-p 80:80进行端口转发</span></span><br><span class="line"><span class="keyword">EXPOSE</span> <span class="number">80</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 申明多个端口</span></span><br><span class="line"><span class="keyword">EXPOSE</span> <span class="number">80</span>,<span class="number">90</span>,<span class="number">100</span></span><br></pre></td></tr></table></figure>
</blockquote>
<h4 id="ENV"><a href="#ENV" class="headerlink" title="ENV"></a>ENV</h4><blockquote>
<p>设置一个环境变量，既可以在镜像内当作变量使用，也可以在容器内部当作环境变量使用，此外，也可以在容器创建时通过-e参数给容器设置环境变量</p>
<figure class="highlight dockerfile"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 设置一个环境变量app_id=123456789</span></span><br><span class="line"><span class="keyword">ENV</span> app_id=<span class="number">123456789</span></span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">通过创建容器时指定环境变量</span></span><br><span class="line">docker --name myapp -e app_id=123456789 -d app</span><br></pre></td></tr></table></figure>
</blockquote>
<h4 id="WORKDIR"><a href="#WORKDIR" class="headerlink" title="WORKDIR"></a>WORKDIR</h4><blockquote>
<p>指定容器的工作目录，这将是后续命令的默认目录</p>
<figure class="highlight dockerfile"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 指定根目录下app目录为工作目录</span></span><br><span class="line"><span class="keyword">WORKDIR</span><span class="language-bash"> /app</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 指定当前路径下app目录为工作目录</span></span><br><span class="line"><span class="keyword">WORKDIR</span><span class="language-bash"> app</span></span><br></pre></td></tr></table></figure>

<p>**注意：**WORKDIR可以设置多次，每次设置都会更改工作目录。</p>
</blockquote>
<h4 id="ADD"><a href="#ADD" class="headerlink" title="ADD"></a>ADD</h4><blockquote>
<p>用于将文件、目录或远程 URL 添加到容器内指定目录中，如果是压缩文件，将会自动解压</p>
<figure class="highlight dockerfile"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 基本用法</span></span><br><span class="line"><span class="keyword">ADD</span><span class="language-bash"> &lt;源路径&gt; &lt;目标路径&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 将Dockerfile当前目录下所有文件和文件夹拷贝到容器内的工作目录，如果没有工作目录，就是容器根目录，也就是Linux根目录</span></span><br><span class="line"><span class="keyword">ADD</span><span class="language-bash"> . .</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 将指定文件拷贝到指定目录</span></span><br><span class="line"><span class="keyword">ADD</span><span class="language-bash"> /usr/local/app.json /home/</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 在指定url上下载文件，拷贝到指定目录</span></span><br><span class="line"><span class="keyword">ADD</span><span class="language-bash"> xxx.xxx.com/user.json /home/user/</span></span><br></pre></td></tr></table></figure>
</blockquote>
<h4 id="COPY"><a href="#COPY" class="headerlink" title="COPY"></a>COPY</h4><blockquote>
<p>用于将文件、目录或远程 URL 拷贝到容器内指定目录中，但是如果是压缩文件，它是不会自动解压的；用法的话和ADD一样，但是不支持远程url。</p>
<p>建议：如果只是简单的拷贝需求，不涉及文件解压的话，建议使用COPY，如果涉及文件解压这样的，就用ADD。</p>
</blockquote>
<h4 id="VOLUME"><a href="#VOLUME" class="headerlink" title="VOLUME"></a>VOLUME</h4><blockquote>
<p>于在容器中创建一个或多个挂载点（卷），挂载点是容器内的目录，可以与主机上的目录或其他容器共享数据。在设置挂载点后，在创建容器时，使用 -v参数将挂载点与宿主机关联起来。</p>
<p>使用方式：</p>
<figure class="highlight dockerfile"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 将/usr/local/myapp目录设置为挂载点，多个挂载点可以编写多个VOLUME指令，或者在一个VOLUME指令中多个挂载点用空格分开</span></span><br><span class="line"><span class="keyword">VOLUME</span><span class="language-bash"> /usr/local/myapp</span></span><br></pre></td></tr></table></figure>

<p>映射方式：</p>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">在创建容器时，将宿主机目录与容器挂载点关联起来</span></span><br><span class="line">docker --name myapp -v /usr/local/app/myapp:/usr/local/myapp -d app-images</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">这样的话容器访问容器内/usr/local/myapp目录相当于访问宿主机/usr/local/app/myapp目录</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">便于容器数据的外挂，配置文件的外挂，日志的外挂，便于容器数据安全</span></span><br></pre></td></tr></table></figure>
</blockquote>
<h4 id="USER"><a href="#USER" class="headerlink" title="USER"></a>USER</h4><blockquote>
<p>用于指定在容器中运行的进程的用户或用户组身份，它可以用于设置容器中运行进程的用户权限和安全性。</p>
<figure class="highlight dockerfile"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 使用示例</span></span><br><span class="line"><span class="keyword">USER</span> &lt;用户名&gt;[:用户组]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 给容器设置用户为fei</span></span><br><span class="line"><span class="keyword">USER</span> fei</span><br><span class="line"></span><br><span class="line"><span class="comment"># 给容器设置用户组为fg</span></span><br><span class="line"><span class="keyword">USER</span> :fg</span><br><span class="line"></span><br><span class="line"><span class="comment"># 给容器设置用户和用户组</span></span><br><span class="line"><span class="keyword">USER</span> fei:fg</span><br></pre></td></tr></table></figure>

<p>**注意：**USER指令指的是切换到USER所指的用户来运行命令，所有要么在镜像构建指令一开始就设置用户，要么在创建容器时使用 -u参数设置用</p>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">docker --name myapp -u fei:fg -d myadd-image</span><br></pre></td></tr></table></figure>
</blockquote>
<h4 id="ARG"><a href="#ARG" class="headerlink" title="ARG"></a>ARG</h4><blockquote>
<p>定义容器中的变量，用法和ENV类似，但是ENV的作用范围可以到镜像外面，给运行起来的容器设置变量，而ARG只作用在镜像内部，充当变量使用，当时可以在创建容器时使用–build-arg对参数进行修改。</p>
<p>所以：当不需要将变量注入到环境变量时，推荐使用ARG，如果要使用到环境变量，推荐使用ENV。</p>
</blockquote>
<h4 id="ONBUILD"><a href="#ONBUILD" class="headerlink" title="ONBUILD"></a>ONBUILD</h4><blockquote>
<p><code>ONBUILD</code> 是 Dockerfile 中的一个指令，用于定义构建触发器，当一个镜像被用作另一个镜像的基础镜像时，基础镜像中的 <code>ONBUILD</code> 指令将会在子镜像的构建过程中触发执行。</p>
<p>什么意思呢，就是在a镜像中设置ONBUILD RUN apt-get update时，然后b镜像使用a镜像作为其基础镜像，当b镜像在构建时，ONBUILD RUN apt-get update这条指令便会被运行，相当于在b镜像中运行RUN apt-get update，ONBUILD这条指令后面可以运行任何Dockerfile指令。</p>
<p>这个指令非常有作用的，在镜像构建时被触发，这样就可以在基础镜像中写一些未来将要触发的命令。</p>
</blockquote>
<h3 id="常见应用构建成Dockerfile"><a href="#常见应用构建成Dockerfile" class="headerlink" title="常见应用构建成Dockerfile"></a>常见应用构建成Dockerfile</h3><h4 id="使用Centos7基础镜像构建java8环境"><a href="#使用Centos7基础镜像构建java8环境" class="headerlink" title="使用Centos7基础镜像构建java8环境"></a>使用Centos7基础镜像构建java8环境</h4><figure class="highlight dockerfile"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 设置centos7作为基础镜像</span></span><br><span class="line"><span class="keyword">FROM</span> centos:<span class="number">7</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置镜像内标签元数据</span></span><br><span class="line"><span class="keyword">LABEL</span><span class="language-bash"> maintainer=<span class="string">&quot;fei&quot;</span></span></span><br><span class="line"><span class="keyword">LABEL</span><span class="language-bash"> version=<span class="string">&quot;1.0.0&quot;</span></span></span><br><span class="line"><span class="keyword">LABEL</span><span class="language-bash"> description=<span class="string">&quot;这是一个java8的Docker镜像&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置工作目录</span></span><br><span class="line"><span class="keyword">WORKDIR</span><span class="language-bash"> /usr/local</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义文件名称</span></span><br><span class="line"><span class="keyword">ARG</span> tar_file=jdk-<span class="number">8</span>u341-linux-x64.tar.gz</span><br><span class="line"></span><br><span class="line"><span class="comment"># 将jdk安装包拷贝到此处（add指令带解压功能）</span></span><br><span class="line"><span class="keyword">COPY</span><span class="language-bash"> <span class="variable">$&#123;tar_file&#125;</span> .</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 重命名文件夹为java8，设置环境变量</span></span><br><span class="line"><span class="keyword">RUN</span><span class="language-bash"> tar -zxvf <span class="variable">$&#123;tar_file&#125;</span>.tar.gz &amp;&amp; \</span></span><br><span class="line"><span class="language-bash">    <span class="built_in">mv</span> jdk1.8.0_341 java8 &amp;&amp; \</span></span><br><span class="line"><span class="language-bash">    <span class="built_in">rm</span> -rf <span class="variable">$&#123;tar_file&#125;</span>.tar.gz</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置java环境变量</span></span><br><span class="line"><span class="keyword">ENV</span> JAVA_HOME=/usr/local/java8</span><br><span class="line"><span class="keyword">ENV</span> PATH=$JAVA_HOME/bin:$PATH</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">CMD</span><span class="language-bash"> [<span class="string">&quot;java&quot;</span>,<span class="string">&quot;-version&quot;</span>]</span></span><br></pre></td></tr></table></figure>

<ul>
<li>在Dockerfile根目录执行命令构建镜像：docker build -t  my-java8 .</li>
<li>通过上面构建的镜像我们发现一些问题，一是使用centos7作为基础镜像的话，centos7的很多功能我们用不到，白白占用了空间；二是我们在Docker容器中一般都是运行编译打包好的代码，以Java项目为例，我们只需要使用到jre；所以这些就是可以优化的地方，下面我使用一个完整的前后端分离项目进行实操，如何通过<strong>多阶段构建</strong>将项目打包部署到Docker容器中。</li>
</ul>
<h4 id="前后端部署到Docker-镜像构建优化（实战）"><a href="#前后端部署到Docker-镜像构建优化（实战）" class="headerlink" title="前后端部署到Docker+镜像构建优化（实战）"></a>前后端部署到Docker+镜像构建优化（实战）</h4><ul>
<li><a target="_blank" rel="noopener" href="https://github.com/tengfei2233/wangBook">项目下载地址</a></li>
<li>注意：以下只是打包，如果需要运行需要更改一些数据源地址，aliyun相关key等，自行抉择</li>
<li>分别在after-end、client、manage目录下编写Dockerfile文件</li>
</ul>
<h5 id="编写after-end后端的Dockerfile"><a href="#编写after-end后端的Dockerfile" class="headerlink" title="编写after-end后端的Dockerfile"></a>编写after-end后端的Dockerfile</h5><ul>
<li>进入after-end目录下，编写Dockerfile</li>
</ul>
<figure class="highlight dockerfile"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 构建after_end，SpringBoot后端代码</span></span><br><span class="line"><span class="comment"># 分阶段构建一，编译打包代码</span></span><br><span class="line"><span class="comment"># 使用官方仓库的mavan3.9镜像</span></span><br><span class="line"><span class="keyword">FROM</span> maven:<span class="number">3.9</span>.<span class="number">3</span>-eclipse-temurin-<span class="number">8</span>-alpine as build</span><br><span class="line"></span><br><span class="line"><span class="comment"># 工作目录/app</span></span><br><span class="line"><span class="keyword">WORKDIR</span><span class="language-bash"> /app</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 拷贝文件进入工作目录</span></span><br><span class="line"><span class="keyword">COPY</span><span class="language-bash"> ./src ./src</span></span><br><span class="line"><span class="keyword">COPY</span><span class="language-bash">  ./pom.xml ./</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 执行maven打包构建</span></span><br><span class="line"><span class="keyword">RUN</span><span class="language-bash"> mvn clean package -DskipTests</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 分阶段构建二，运行jar包</span></span><br><span class="line"><span class="keyword">FROM</span> openjdk:<span class="number">8</span>u212-jre-alpine3.<span class="number">9</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 工作目录app</span></span><br><span class="line"><span class="keyword">WORKDIR</span><span class="language-bash"> /app</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 从第一个阶段拷贝jar过来</span></span><br><span class="line"><span class="keyword">COPY</span><span class="language-bash"> --from=build /app/target/after-end-1.0.jar .</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">CMD</span><span class="language-bash"> [ <span class="string">&quot;java&quot;</span> ,<span class="string">&quot;-jar&quot;</span> , <span class="string">&quot;after-end-1.0.jar&quot;</span>]</span></span><br></pre></td></tr></table></figure>

<ul>
<li>运行脚本打包脚本：docker build -t after-end .</li>
</ul>
<h5 id="编写front-client前端用户端Dockerfile"><a href="#编写front-client前端用户端Dockerfile" class="headerlink" title="编写front-client前端用户端Dockerfile"></a>编写front-client前端用户端Dockerfile</h5><ul>
<li>进入client目录下，编写Dockerfile</li>
</ul>
<figure class="highlight dockerfile"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 构建front-clien，Vue前端前台代码</span></span><br><span class="line"><span class="comment"># 分阶段构建一，安装依赖，打包代码</span></span><br><span class="line"><span class="keyword">FROM</span> node:<span class="number">14.21</span>.<span class="number">3</span>-alpine3.<span class="number">16</span> as build</span><br><span class="line"></span><br><span class="line"><span class="comment"># 指定容器元标签</span></span><br><span class="line"><span class="keyword">LABEL</span><span class="language-bash"> maintainer=<span class="string">&quot;tengfei&quot;</span></span></span><br><span class="line"><span class="keyword">LABEL</span><span class="language-bash"> version=<span class="string">&quot;1.0.0&quot;</span></span></span><br><span class="line"><span class="keyword">LABEL</span><span class="language-bash"> description=<span class="string">&quot;wangBook的用户端前端&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置工作目录</span></span><br><span class="line"><span class="keyword">WORKDIR</span><span class="language-bash"> /app</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 拷贝代码</span></span><br><span class="line"><span class="keyword">COPY</span><span class="language-bash"> ./src ./src</span></span><br><span class="line"><span class="keyword">COPY</span><span class="language-bash"> ./public ./public</span></span><br><span class="line"><span class="keyword">COPY</span><span class="language-bash"> package.json .env.production jsconfig.json vue.config.js babel.config.js ./</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 下载依赖，打包</span></span><br><span class="line"><span class="keyword">RUN</span><span class="language-bash"> npm install --registry=https://registry.npm.taobao.org/ &amp;&amp; \</span></span><br><span class="line"><span class="language-bash">    npm run build</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 分阶段构建二，将打包好的代码带nginx上运行</span></span><br><span class="line"><span class="keyword">FROM</span> nginx:<span class="number">1.22</span>.<span class="number">1</span>-alpine-slim</span><br><span class="line"></span><br><span class="line"><span class="comment">#拷贝打包好的文件到文件夹</span></span><br><span class="line"><span class="keyword">COPY</span><span class="language-bash"> --from=build /app/dist /usr/share/nginx/html</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 将本地nginx.conf拷贝上去，可选，需要自定义nginx配置可选</span></span><br><span class="line"><span class="comment"># COPY nginx.conf /etc/nginx/nginx.conf</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 暴露端口</span></span><br><span class="line"><span class="keyword">EXPOSE</span> <span class="number">80</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 启动nginx，这个的daemon off是停止守护线程模型，就是不在后台运行，要不然容器会停止</span></span><br><span class="line"><span class="keyword">CMD</span><span class="language-bash"> [<span class="string">&quot;nginx&quot;</span>, <span class="string">&quot;-g&quot;</span>,<span class="string">&quot;daemon off;&quot;</span>]</span></span><br></pre></td></tr></table></figure>

<ul>
<li>运行脚本打包脚本：docker build -t front-client .</li>
</ul>
<h5 id="编写front-manage前端管理端Dockerfile"><a href="#编写front-manage前端管理端Dockerfile" class="headerlink" title="编写front-manage前端管理端Dockerfile"></a>编写front-manage前端管理端Dockerfile</h5><ul>
<li>进入manage目录下，编写Dockerfile</li>
</ul>
<figure class="highlight dockerfile"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 构建front-clien，Vue前端前台代码</span></span><br><span class="line"><span class="comment"># 分阶段构建一，安装依赖，打包代码</span></span><br><span class="line"><span class="keyword">FROM</span> node:<span class="number">14.21</span>.<span class="number">3</span>-alpine3.<span class="number">16</span> as build</span><br><span class="line"></span><br><span class="line"><span class="comment"># 指定容器元标签</span></span><br><span class="line"><span class="keyword">LABEL</span><span class="language-bash"> maintainer=<span class="string">&quot;tengfei&quot;</span></span></span><br><span class="line"><span class="keyword">LABEL</span><span class="language-bash"> version=<span class="string">&quot;1.0.0&quot;</span></span></span><br><span class="line"><span class="keyword">LABEL</span><span class="language-bash"> description=<span class="string">&quot;wangBook的用户端管理端&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置工作目录</span></span><br><span class="line"><span class="keyword">WORKDIR</span><span class="language-bash"> /app</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 拷贝代码</span></span><br><span class="line"><span class="keyword">COPY</span><span class="language-bash"> ./src ./src</span></span><br><span class="line"><span class="keyword">COPY</span><span class="language-bash"> ./public ./public</span></span><br><span class="line"><span class="keyword">COPY</span><span class="language-bash"> package.json .env.production jsconfig.json vue.config.js babel.config.js ./</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 下载依赖，打包</span></span><br><span class="line"><span class="keyword">RUN</span><span class="language-bash"> npm install --registry=https://registry.npm.taobao.org/ &amp;&amp; \</span></span><br><span class="line"><span class="language-bash">    npm run build</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 分阶段构建二，将打包好的代码带nginx上运行</span></span><br><span class="line"><span class="keyword">FROM</span> nginx:<span class="number">1.22</span>.<span class="number">1</span>-alpine-slim</span><br><span class="line"></span><br><span class="line"><span class="comment">#拷贝打包好的文件到文件夹</span></span><br><span class="line"><span class="keyword">COPY</span><span class="language-bash"> --from=build /app/dist /usr/share/nginx/html</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 将本地nginx.conf拷贝上去，可选，需要自定义nginx配置可选</span></span><br><span class="line"><span class="comment"># COPY nginx.conf /etc/nginx/nginx.conf</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 暴露端口</span></span><br><span class="line"><span class="keyword">EXPOSE</span> <span class="number">80</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 启动nginx，这个的daemon off是停止守护线程模型，就是不在后台运行，要不然容器会停止</span></span><br><span class="line"><span class="keyword">CMD</span><span class="language-bash"> [<span class="string">&quot;nginx&quot;</span>, <span class="string">&quot;-g&quot;</span>,<span class="string">&quot;daemon off;&quot;</span>]</span></span><br></pre></td></tr></table></figure>

<ul>
<li>运行脚本打包脚本：docker build -t front-manage .</li>
</ul>
<h5 id="关于Dockerfile的，先到这里了，以后有缺失再补充"><a href="#关于Dockerfile的，先到这里了，以后有缺失再补充" class="headerlink" title="关于Dockerfile的，先到这里了，以后有缺失再补充"></a>关于Dockerfile的，先到这里了，以后有缺失再补充</h5></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://tengfei2233.gitee.io">Fei</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://tengfei2233.gitee.io/2023/8/27/Dockerfile%E5%AD%A6%E4%B9%A0/">https://tengfei2233.gitee.io/2023/8/27/Dockerfile%E5%AD%A6%E4%B9%A0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://tengfei2233.gitee.io" target="_blank">飞の博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Docker/">Docker</a></div><div class="post_share"><div class="social-share" data-image="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-6-26/docker.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.staticfile.org/butterfly-extsrc/1.1.3/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.staticfile.org/butterfly-extsrc/1.1.3/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/common/wechat.png" target="_blank"><img class="post-qr-code-img" src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/common/wechat.png" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/common/alipay.png" target="_blank"><img class="post-qr-code-img" src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/common/alipay.png" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/8/27/Docker%E5%9F%BA%E7%A1%80%E5%AD%A6%E4%B9%A0%E6%96%87%E6%A1%A3-%E5%8C%85%E6%8B%AC%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B/" title="Docker基础学习文档(包括安装教程)"><img class="cover" src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-6-26/docker.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Docker基础学习文档(包括安装教程)</div></div></a></div><div class="next-post pull-right"><a href="/2023/8/27/Docker-Compose%E5%AD%A6%E4%B9%A0/" title="Docker Compose学习"><img class="cover" src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-6-26/docker.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Docker Compose学习</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2023/8/27/Docker%E5%9F%BA%E7%A1%80%E5%AD%A6%E4%B9%A0%E6%96%87%E6%A1%A3-%E5%8C%85%E6%8B%AC%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B/" title="Docker基础学习文档(包括安装教程)"><img class="cover" src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-6-26/docker.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2023-08-27</div><div class="title">Docker基础学习文档(包括安装教程)</div></div></a></div><div><a href="/2023/6/26/Docker%E5%AE%89%E8%A3%85%E4%B8%80%E4%BA%9B%E5%B8%B8%E7%94%A8%E8%BD%AF%E4%BB%B6%EF%BC%88%E6%9B%B4%E6%96%B0%E4%B8%AD-%EF%BC%89/" title="Docker安装一些常用软件（更新中！）"><img class="cover" src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-6-26/docker.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2023-07-31</div><div class="title">Docker安装一些常用软件（更新中！）</div></div></a></div><div><a href="/2023/8/27/Docker-Compose%E5%AD%A6%E4%B9%A0/" title="Docker Compose学习"><img class="cover" src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-6-26/docker.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2023-08-27</div><div class="title">Docker Compose学习</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/common/fei.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Fei</div><div class="author-info__description">当你想起来了，那就记录一点吧</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">24</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">28</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">16</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/tengfei2233"><i class="fab fa-github"></i><span>关注我</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/tengfei2233" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://gitee.com/tengfei2233" target="_blank" title="Gitee"><i class="fab fa-git"></i></a><a class="social-icon" href="/youfeige@outlook.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">当你迷茫的时候，那就静下心想想吧！</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Dockerfile%E5%AD%A6%E4%B9%A0"><span class="toc-number">1.</span> <span class="toc-text">Dockerfile学习</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%B8%8B%E4%B8%80%E7%AF%87Docker%E6%96%87%E7%AB%A0%EF%BC%9ADocker-Compose%E5%AD%A6%E4%B9%A0"><span class="toc-number">1.0.0.1.</span> <span class="toc-text">下一篇Docker文章：Docker Compose学习</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AFDockerfile"><span class="toc-number">1.1.</span> <span class="toc-text">什么是Dockerfile</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Dockerfile%E6%8C%87%E4%BB%A4%E8%AF%A6%E8%A7%A3"><span class="toc-number">1.2.</span> <span class="toc-text">Dockerfile指令详解</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#FROM"><span class="toc-number">1.2.1.</span> <span class="toc-text">FROM</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#MAINTAINER%EF%BC%88%E5%B7%B2%E5%BC%83%E7%94%A8%EF%BC%89-x2F-LABEL"><span class="toc-number">1.2.2.</span> <span class="toc-text">MAINTAINER（已弃用）&#x2F; LABEL</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#RUN"><span class="toc-number">1.2.3.</span> <span class="toc-text">RUN</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#CMD"><span class="toc-number">1.2.4.</span> <span class="toc-text">CMD</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ENTRYPOINT"><span class="toc-number">1.2.5.</span> <span class="toc-text">ENTRYPOINT</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#EXPOSE"><span class="toc-number">1.2.6.</span> <span class="toc-text">EXPOSE</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ENV"><span class="toc-number">1.2.7.</span> <span class="toc-text">ENV</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#WORKDIR"><span class="toc-number">1.2.8.</span> <span class="toc-text">WORKDIR</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ADD"><span class="toc-number">1.2.9.</span> <span class="toc-text">ADD</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#COPY"><span class="toc-number">1.2.10.</span> <span class="toc-text">COPY</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#VOLUME"><span class="toc-number">1.2.11.</span> <span class="toc-text">VOLUME</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#USER"><span class="toc-number">1.2.12.</span> <span class="toc-text">USER</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ARG"><span class="toc-number">1.2.13.</span> <span class="toc-text">ARG</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ONBUILD"><span class="toc-number">1.2.14.</span> <span class="toc-text">ONBUILD</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E5%BA%94%E7%94%A8%E6%9E%84%E5%BB%BA%E6%88%90Dockerfile"><span class="toc-number">1.3.</span> <span class="toc-text">常见应用构建成Dockerfile</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8Centos7%E5%9F%BA%E7%A1%80%E9%95%9C%E5%83%8F%E6%9E%84%E5%BB%BAjava8%E7%8E%AF%E5%A2%83"><span class="toc-number">1.3.1.</span> <span class="toc-text">使用Centos7基础镜像构建java8环境</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%89%8D%E5%90%8E%E7%AB%AF%E9%83%A8%E7%BD%B2%E5%88%B0Docker-%E9%95%9C%E5%83%8F%E6%9E%84%E5%BB%BA%E4%BC%98%E5%8C%96%EF%BC%88%E5%AE%9E%E6%88%98%EF%BC%89"><span class="toc-number">1.3.2.</span> <span class="toc-text">前后端部署到Docker+镜像构建优化（实战）</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%BC%96%E5%86%99after-end%E5%90%8E%E7%AB%AF%E7%9A%84Dockerfile"><span class="toc-number">1.3.2.1.</span> <span class="toc-text">编写after-end后端的Dockerfile</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%BC%96%E5%86%99front-client%E5%89%8D%E7%AB%AF%E7%94%A8%E6%88%B7%E7%AB%AFDockerfile"><span class="toc-number">1.3.2.2.</span> <span class="toc-text">编写front-client前端用户端Dockerfile</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%BC%96%E5%86%99front-manage%E5%89%8D%E7%AB%AF%E7%AE%A1%E7%90%86%E7%AB%AFDockerfile"><span class="toc-number">1.3.2.3.</span> <span class="toc-text">编写front-manage前端管理端Dockerfile</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%85%B3%E4%BA%8EDockerfile%E7%9A%84%EF%BC%8C%E5%85%88%E5%88%B0%E8%BF%99%E9%87%8C%E4%BA%86%EF%BC%8C%E4%BB%A5%E5%90%8E%E6%9C%89%E7%BC%BA%E5%A4%B1%E5%86%8D%E8%A1%A5%E5%85%85"><span class="toc-number">1.3.2.4.</span> <span class="toc-text">关于Dockerfile的，先到这里了，以后有缺失再补充</span></a></li></ol></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/11/21/netty%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8%EF%BC%8C%E5%9F%BA%E6%9C%AC%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90%EF%BC%8C%E7%9B%B8%E5%85%B3%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3-%E6%9B%B4%E6%96%B0%E4%B8%AD/" title="netty的基本使用，基本原理分析，相关问题解(更新中...)"><img src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-11-10/netty-ai.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="netty的基本使用，基本原理分析，相关问题解(更新中...)"/></a><div class="content"><a class="title" href="/2023/11/21/netty%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8%EF%BC%8C%E5%9F%BA%E6%9C%AC%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90%EF%BC%8C%E7%9B%B8%E5%85%B3%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3-%E6%9B%B4%E6%96%B0%E4%B8%AD/" title="netty的基本使用，基本原理分析，相关问题解(更新中...)">netty的基本使用，基本原理分析，相关问题解(更新中...)</a><time datetime="2023-11-21T09:27:51.000Z" title="发表于 2023-11-21 17:27:51">2023-11-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/11/10/%E5%9C%A8conda%E7%8E%AF%E5%A2%83%E4%B8%8B%E8%BF%90%E8%A1%8Cstable-diffusion-webui-AI%E7%BB%98%E5%9B%BE/" title="在conda环境下运行stable-diffusion-webui AI绘图"><img src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-11-10/ai.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="在conda环境下运行stable-diffusion-webui AI绘图"/></a><div class="content"><a class="title" href="/2023/11/10/%E5%9C%A8conda%E7%8E%AF%E5%A2%83%E4%B8%8B%E8%BF%90%E8%A1%8Cstable-diffusion-webui-AI%E7%BB%98%E5%9B%BE/" title="在conda环境下运行stable-diffusion-webui AI绘图">在conda环境下运行stable-diffusion-webui AI绘图</a><time datetime="2023-11-10T07:30:51.000Z" title="发表于 2023-11-10 15:30:51">2023-11-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/11/10/%E5%9C%A8Ubuntu20%E4%B8%8A%E5%AE%89%E8%A3%85CUDA-cuDNN-Miniconda%E7%8E%AF%E5%A2%83(%E5%85%B6%E5%AE%83%E7%B3%BB%E7%BB%9F%E5%90%8C%E7%90%86)/" title="在Ubuntu20上安装CUDA+cuDNN+Miniconda环境(其它系统同理)"><img src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-11-10/ai.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="在Ubuntu20上安装CUDA+cuDNN+Miniconda环境(其它系统同理)"/></a><div class="content"><a class="title" href="/2023/11/10/%E5%9C%A8Ubuntu20%E4%B8%8A%E5%AE%89%E8%A3%85CUDA-cuDNN-Miniconda%E7%8E%AF%E5%A2%83(%E5%85%B6%E5%AE%83%E7%B3%BB%E7%BB%9F%E5%90%8C%E7%90%86)/" title="在Ubuntu20上安装CUDA+cuDNN+Miniconda环境(其它系统同理)">在Ubuntu20上安装CUDA+cuDNN+Miniconda环境(其它系统同理)</a><time datetime="2023-11-10T07:27:51.000Z" title="发表于 2023-11-10 15:27:51">2023-11-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/8/27/Docker-Compose%E5%AD%A6%E4%B9%A0/" title="Docker Compose学习"><img src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-6-26/docker.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Docker Compose学习"/></a><div class="content"><a class="title" href="/2023/8/27/Docker-Compose%E5%AD%A6%E4%B9%A0/" title="Docker Compose学习">Docker Compose学习</a><time datetime="2023-08-27T15:34:50.000Z" title="发表于 2023-08-27 23:34:50">2023-08-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/8/27/Dockerfile%E5%AD%A6%E4%B9%A0/" title="Dockerfile学习"><img src="https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-6-26/docker.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Dockerfile学习"/></a><div class="content"><a class="title" href="/2023/8/27/Dockerfile%E5%AD%A6%E4%B9%A0/" title="Dockerfile学习">Dockerfile学习</a><time datetime="2023-08-27T15:34:37.000Z" title="发表于 2023-08-27 23:34:37">2023-08-27</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://blog-1306275892.cos.ap-shanghai.myqcloud.com/hexo/blogs/2023-6-26/docker.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2024 By Fei</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">欢迎进入<a href="tengfei2233.gitee.io/">飞の博客</a>!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="https://cdn.staticfile.org/hexo-theme-butterfly/4.7.0/js/utils.min.js"></script><script src="https://cdn.staticfile.org/hexo-theme-butterfly/4.7.0/js/main.min.js"></script><script src="https://cdn.staticfile.org/hexo-theme-butterfly/4.7.0/js/tw_cn.min.js"></script><script src="https://cdn.staticfile.org/fancyapps-ui/4.0.31/fancybox.umd.min.js"></script><script src="https://cdn.staticfile.org/instant.page/5.1.1/instantpage.min.js" type="module"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.staticfile.org/pangu/4.0.7/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: '0kbeJfFlvhvkYfqMqwwVCx1a-gzGzoHsz',
      appKey: 'rziWUw1MTHMW7d9eeCQzu9HV',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.staticfile.org/valine/1.5.1/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !true) {
  if (true) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><script defer="defer" id="ribbon" src="https://cdn.staticfile.org/butterfly-extsrc/1.1.3/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div><script src="https://cdn.staticfile.org/hexo-theme-butterfly/4.7.0/js/search/local-search.min.js"></script></div></body></html>